import fs from 'fs';
import data from './fantasyData.js';
import path from 'path';
import { AppName } from '../app.config.js';
import { plugin } from "alemon";

//定义一个版本信息的常量,获取默认文件配置文件信息

/**
 * 全局
 */
//插件根目录
const __dirname = path.resolve() + path.sep + 'plugins' + path.sep + AppName;
// 文件存放路径
export const __PATH = {
  //更新日志
  updata_log_path: path.join(__dirname, 'vertion.txt'),
  //用户数据
  player_path: path.join(__dirname, '/resources/data/player'),
  //装备
  equipment_path: path.join(__dirname, '/resources/data/equipment'),
  //背包
  bag_path: path.join(__dirname, '/resources/data/bag'),
  lib_path: path.join(__dirname, '/resources/data/lib'),
  map_path: path.join(__dirname, '/resources/data/map'),
  battle_path: path.join(__dirname, '/resources/data/battle'),
};

//检查存档是否存在，存在返回true;
export async function existplayer(usr_qq) {
  let exist_player;
  exist_player = fs.existsSync(`${__PATH.player_path}/${usr_qq}.json`);
  if (exist_player) {
    return true;
  }
  return false;
}

//读取存档信息，返回成一个JavaScript对象
export async function Read_player(usr_qq) {
  let playerPath =`${__PATH.player_path}/${usr_qq}.json`
  //根据长度判读是否是分身
  let dir = path.join(playerPath);
  let player = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  player = JSON.parse(player);
  return player;
}

export async function Read_bag(usr_qq) {
  let dir = path.join(`${__PATH.bag_path}/${usr_qq}.json`);
  let bag = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  bag = JSON.parse(bag);
  return bag;
}

export async function Num_bag(usr_qq,thing) {
  let dir = path.join(`${__PATH.bag_path}/${usr_qq}.json`);
  let bag = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  bag = JSON.parse(bag);
  const type = await Read_thingtype(thing);
  if(type == false){
    return false;
  }
  let thing0 = bag[type].find(obj => obj["名称"] == thing)
  let number = thing0["数量"];
  return number;
}

//读取玩家探索事件map_event
export async function Read_mapevent(map) {
  let dir = path.join(`${__PATH.map_path}/map_event/${map}.json`);
  let event = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  event = JSON.parse(event);
  return event;
}

//读取玩家移动事件map_move
export async function Read_mapmove(map) {
  let dir = path.join(`${__PATH.map_path}/map_move/${map}.json`);
  let event = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  event = JSON.parse(event);
  return event;
}

//写入玩家信息,第二个参数是一个JavaScript对象
export async function Write_player(usr_qq, player) {
  let dir = path.join(__PATH.player_path, `${usr_qq}.json`);
  //根据长度判读是否是分身
  let new_ARR = JSON.stringify(player, '', '\t');
  fs.writeFileSync(dir, new_ARR, 'utf8', err => {
    console.log('写入成功', err);
  });
  return;
}

//读取怪物列表信息
export async function Read_monster(name) {
  let dir = path.join(`${__PATH.lib_path}/怪物列表/${name}.json`);
  let monster = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  monster = JSON.parse(monster);
  return monster;
}

//加载战斗对象
export async function Set_battleobject(usr_qq, object, level) {
  const directoryPath = path.join(__PATH.battle_path, "战斗对象");
  const filePath = path.join(directoryPath, `${usr_qq}.json`);
  //不存在战斗数据则创建
  if (!fs.existsSync(filePath)) {
  const data0 = {};
  fs.writeFileSync(filePath, JSON.stringify(data0));
  }
  //读取战斗数据
  let data = fs.readFileSync(filePath, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  data = JSON.parse(data);
  //计算战斗怪物属性
  let monster = await Read_monster(object);
  data["名称"] = monster["名称"];
  const range = level;
  const arr = range.split("-");
  const min = Number(arr[0]);
  const max = Number(arr[1]);
  const level1 = Math.floor(Math.random() * (max - min + 1)) + min;
  data["等级"] = level1;
  data["体质"] = Math.floor((monster["体质基础"] + monster["体质成长"] * Math.pow(level1, monster["成长系数"])) * (0.8 + 0.7 * Math.random()));
  data["精神"] = Math.floor((monster["精神基础"] + monster["精神成长"] * Math.pow(level1, monster["成长系数"])) * (0.8 + 0.7 * Math.random()));
  data["力量"] = Math.floor((monster["力量基础"] + monster["力量成长"] * Math.pow(level1, monster["成长系数"])) * (0.8 + 0.7 * Math.random()));
  data["智力"] = Math.floor((monster["智力基础"] + monster["智力成长"] * Math.pow(level1, monster["成长系数"])) * (0.8 + 0.7 * Math.random()));
  data["敏捷"] = Math.floor((monster["敏捷基础"] + monster["敏捷成长"] * Math.pow(level1, monster["成长系数"])) * (0.8 + 0.7 * Math.random()));
  data["幸运"] = Math.floor((monster["幸运基础"] + monster["幸运成长"] * Math.pow(level1, monster["成长系数"])) * (0.8 + 0.7 * Math.random()));
  data["经验"] = Math.floor((monster["经验基础"] + monster["经验成长"] * Math.pow(level1, monster["成长系数"])) * (0.8 + 0.7 * Math.random()));
  data["生命上限"] = Math.floor(data["体质"] * 10 + data["精神"] * 3 + data["力量"] * 1);
  data["魔力上限"] = Math.floor(data["精神"] * 10 + data["体质"] * 3 + data["智力"] * 1);
  data["当前生命"] = data["生命上限"];
  data["当前魔力"] = data["魔力上限"];
  data["物攻"] = Math.floor(data["力量"] * 2 + data["体质"] * 0.6 + data["敏捷"] * 0.2);
  data["魔攻"] = Math.floor(data["智力"] * 2 + data["精神"] * 0.6 + data["敏捷"] * 0.2);
  data["物防"] = Math.floor(data["体质"] * 1 + data["幸运"] * 0.3 + data["力量"] * 0.1);
  data["魔防"] = Math.floor(data["精神"] * 1 + data["幸运"] * 0.3 + data["智力"] * 0.1);
  data["命中"] = Math.floor(data["敏捷"] * 1 + data["幸运"] * 0.3 + data["精神"] * 0.1);
  data["闪避"] = Math.floor(data["敏捷"] * 1 + data["幸运"] * 0.3 + data["精神"] * 0.1);
  data["暴击"] = Math.floor(data["幸运"] * 1 + data["敏捷"] * 0.3 + data["智力"] * 0.1);
  data["暴伤"] = Math.floor(data["幸运"] * 1 + data["敏捷"] * 0.3 + data["力量"] * 0.1);
  data["暴免"] = Math.floor(data["精神"] * 1 + data["幸运"] * 0.3 + data["智力"] * 0.1);
  data["暴抗"] = Math.floor(data["体质"] * 1 + data["幸运"] * 0.3 + data["力量"] * 0.1);
  data["韧性"] = Math.floor(data["精神"] * 1 + data["体质"] * 0.3 + data["敏捷"] * 0.1);
  data["速度"] = Math.floor(data["敏捷"] * 1 + data["精神"] * 0.3 + data["幸运"] * 0.1);
  data["技能"] = monster["技能"];
  const num = data["技能"].length;
  if(num > 0){
    for(let i = 0; i < num; i++){
      data["技能"][i]["时间"] = 0;
    }
  }
  data["掉落"] = monster["掉落"];  
  data["当前技能"] = "";
  data["增益"] = [];
  data["状态"] = [];
  data["回合"] = 1;
  if(monster["舞台"] !== undefined){
    data["舞台"] = monster["舞台"];
  }
  let new_data = JSON.stringify(data, '', '\t');
  fs.writeFileSync(filePath, new_data, 'utf8', err => {
    console.log('写入成功', err);
  });
  return;

}

//读取装备信息
export async function Read_equipment(usr_qq) {
  let dir = path.join(`${__PATH.equipment_path}/${usr_qq}.json`);
  let equipment = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  equipment = JSON.parse(equipment);
  return equipment;
}

//写入装备信息
export async function Write_equipment(usr_qq, equipment) {
  let player = await Read_player(usr_qq);
  player.体质 = Math.floor(((100 + player.等级 * 10) * player.体质潜力) * (1 + 0.002 * player.天赋));
  player.精神 = Math.floor(((100 + player.等级 * 10) * player.精神潜力) * (1 + 0.002 * player.天赋));
  player.力量 = Math.floor(((100 + player.等级 * 10) * player.力量潜力) * (1 + 0.002 * player.天赋));
  player.智力 = Math.floor(((100 + player.等级 * 10) * player.智力潜力) * (1 + 0.002 * player.天赋));
  player.敏捷 = Math.floor(((100 + player.等级 * 10) * player.敏捷潜力) * (1 + 0.002 * player.天赋));
  player.幸运 = Math.floor(((100 + player.等级 * 10) * player.幸运潜力) * (1 + 0.002 * player.天赋));
  player.生命上限 = Math.floor(player.体质 * 10 + player.精神 * 3 + player.力量 * 1);
  player.魔力上限 = Math.floor(player.精神 * 10 + player.体质 * 3 + player.智力 * 1);
  player.物攻 = Math.floor(player.力量 * 2 + player.体质 * 0.6 + player.敏捷 * 0.2);
  player.魔攻 = Math.floor(player.智力 * 2 + player.精神 * 0.6 + player.敏捷 * 0.2);
  player.物防 = Math.floor(player.体质 * 1 + player.幸运 * 0.3 + player.力量 * 0.1);
  player.魔防 = Math.floor(player.精神 * 1 + player.幸运 * 0.3 + player.智力 * 0.1);
  player.命中 = Math.floor(player.敏捷 * 1 + player.幸运 * 0.3 + player.精神 * 0.1);
  player.闪避 = Math.floor(player.敏捷 * 1 + player.幸运 * 0.3 + player.精神 * 0.1);
  player.暴击 = Math.floor(player.幸运 * 1 + player.敏捷 * 0.3 + player.智力 * 0.1);
  player.暴伤 = Math.floor(player.幸运 * 1 + player.敏捷 * 0.3 + player.力量 * 0.1);
  player.暴免 = Math.floor(player.精神 * 1 + player.幸运 * 0.3 + player.智力 * 0.1);
  player.暴抗 = Math.floor(player.体质 * 1 + player.幸运 * 0.3 + player.力量 * 0.1);
  player.韧性 = Math.floor(player.精神 * 1 + player.体质 * 0.3 + player.敏捷 * 0.1);
  player.速度 = Math.floor(player.敏捷 * 1 + player.精神 * 0.3 + player.幸运 * 0.1);
  player.移速 = Math.floor(Math.pow(player.速度 / 100, 0.5));
  let type = ['武器', '副手', '头肩', '上装', '腰带', '下装', '鞋子', '戒指', '手镯', '项链'];
  for (let i of type) {
      if (Math.abs(equipment[i].物攻) > 1){
      player.物攻 += equipment[i].物攻;
      }
      if(Math.abs(equipment[i].魔攻) > 1){
      player.魔攻 += equipment[i].魔攻;
      }
      if(Math.abs(equipment[i].物防) > 1){
      player.物防 += equipment[i].物防;
      }
      if(Math.abs(equipment[i].魔防) > 1){
      player.魔防 += equipment[i].魔防;
      }
      if(Math.abs(equipment[i].生命上限) > 1){
      player.生命上限 += equipment[i].生命上限;
      }
      if(Math.abs(equipment[i].魔力上限) > 1){
      player.魔力上限 += equipment[i].魔力上限;
      }
      if(Math.abs(equipment[i].命中) > 1){
      player.命中 += equipment[i].命中;
      }
      if(Math.abs(equipment[i].闪避) > 1){
      player.闪避 += equipment[i].闪避;
      }
      if(Math.abs(equipment[i].暴击) > 1){
      player.暴击 += equipment[i].暴击;
      }
      if(Math.abs(equipment[i].暴伤) > 1){
      player.暴伤 += equipment[i].暴伤;
      }
      if(Math.abs(equipment[i].暴免) > 1){
      player.暴免 += equipment[i].暴免;
      }
      if(Math.abs(equipment[i].暴抗) > 1){
      player.暴抗 += equipment[i].暴抗;
      }
      if(Math.abs(equipment[i].韧性) > 1){
      player.韧性 += equipment[i].韧性;
      }
      if(Math.abs(equipment[i].速度) > 1){
      player.速度 += equipment[i].速度;
      }
  }
  await Write_player(usr_qq, player);
  await Add_HP(usr_qq, 0);
  let dir = path.join(__PATH.equipment_path, `${usr_qq}.json`);
  let new_ARR = JSON.stringify(equipment, '', '\t');
  fs.writeFileSync(dir, new_ARR, 'utf8', err => {
    console.log('写入成功', err);
  });
  return;
}

//加载战斗属性
export async function Set_battleplayer(usr_qq) {
  const directoryPath = path.join(__PATH.battle_path, "战斗属性");
  const filePath = path.join(directoryPath, `${usr_qq}.json`);
  //不存在战斗数据则创建
  if (!fs.existsSync(filePath)) {
  const data0 = {};
  fs.writeFileSync(filePath, JSON.stringify(data0));
  }
  //读取战斗数据
  let data = fs.readFileSync(filePath, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  data = JSON.parse(data);
  //计算战斗人物属性
  let player = await Read_player(usr_qq);
  data["名称"] = player["昵称"];
  data["生命上限"] = player["生命上限"];
  data["魔力上限"] = player["魔力上限"];
  data["当前生命"] = player["当前生命"];
  data["当前魔力"] = player["当前魔力"];
  data["物攻"] = player["物攻"];
  data["魔攻"] = player["魔攻"];
  data["物防"] = player["物防"];
  data["魔防"] = player["魔防"];
  data["命中"] = player["命中"];
  data["闪避"] = player["闪避"];
  data["暴击"] = player["暴击"];
  data["暴伤"] = player["暴伤"];
  data["暴免"] = player["暴免"];
  data["暴抗"] = player["暴抗"];
  data["韧性"] = player["韧性"];
  data["速度"] = player["速度"];
  data["技能"] = player["技能"];
  const num = data["技能"].length;
  if(num > 0){
    for(let i = 0; i < num; i++){
      data["技能"][i]["时间"] = 0;
    }
  }
  data["当前技能"] = "";
  data["增益"] = [];
  data["状态"] = [];
  data["回合"] = 1;
  let new_data = JSON.stringify(data, '', '\t');
  fs.writeFileSync(filePath, new_data, 'utf8', err => {
    console.log('写入成功', err);
  });
  return;
}

//写入背包信息
export async function Write_bag(usr_qq, bag) {
  let dir = path.join(__PATH.bag_path, `${usr_qq}.json`);
  let new_ARR = JSON.stringify(bag, '', '\t');
  fs.writeFileSync(dir, new_ARR, 'utf8', err => {
    console.log('写入成功', err);
  });
  return;
}
//增加当前生命
export async function Add_HP(usr_qq, blood = 0) {
  let player = await Read_player(usr_qq);
  player.当前生命 += Math.trunc(blood);
  if (player.当前生命 > player.当前生命) {
    player.当前生命 = player.当前生命;
  }
  if (player.当前生命 < 0) {
    player.当前生命 = 0;
  }
  await Write_player(usr_qq, player);
  return;
}

//读玩家状态
export async function Read_state(usr_qq) {
  let player = await Read_player(usr_qq);
  let id = player["id"];
  let dir = path.join(`${__PATH.player_path}/玩家状态.json`);
  let state = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
  return data;
  });
  //将字符串数据转变成数组格式
  state = JSON.parse(state);
  state = state[id];
  return state;
}

//写玩家状态
export async function Write_state(usr_qq, state0) {
  let player = await Read_player(usr_qq);
  let id = player["id"];
  let dir = path.join(`${__PATH.player_path}/玩家状态.json`);
  let state = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
  return data;
  });
  state = JSON.parse(state);
  state[id] = state0;
  let new_ARR = JSON.stringify(state, '', '\t');
  fs.writeFileSync(dir, new_ARR, 'utf8', err => {
    console.log('写入成功', err);
  });
  return;
}

//增加当前魔力
export async function Add_MP(usr_qq, mp = 0) {
  let player = await Read_player(usr_qq);
  player.当前魔力 += Math.trunc(mp);
  if (player.当前魔力 > player.当前魔力) {
    player.当前魔力 = player.当前魔力;
  }
  if (player.当前魔力 < 0) {
    player.当前魔力 = 0;
  }
  await Write_player(usr_qq, player);
  return;
}

//判断升级
export async function Judge_up(usr_qq, e){
let player = await Read_player(usr_qq);
let dengji = player.等级;
let level = data.level_list;
let level_max = level[dengji].经验;
let equ = await Read_equipment(usr_qq);
if (player.经验 >= level_max){
  player.等级 += 1;
  player.经验 -= level_max;
  await Write_player(usr_qq,player);
  await Write_equipment(usr_qq, equ);
  player = await Read_player(usr_qq);
  player["当前生命"] = player["生命上限"];
  player["当前魔力"] = player["魔力上限"];
  await Write_player(usr_qq,player);
  e.reply(`⬆⬆⬆ Level UP ⬆⬆⬆\n[${player.昵称}]等级提升至${player.等级}级\n发送[属性]查看自身属性`)
  await Judge_up(usr_qq, e)
}else{
  await Write_equipment(usr_qq, equ);
  return;
}
}

//读取物品类别
export async function Read_thingtype(thing_name) {
  const dirs = [
    path.join(`${__PATH.lib_path}/物品列表/材料列表.json`),
    path.join(`${__PATH.lib_path}/物品列表/道具列表.json`),
    path.join(`${__PATH.lib_path}/物品列表/装备列表.json`),
    path.join(`${__PATH.lib_path}/物品列表/技能书列表.json`)
  ];
 function findNameOne() {
  for (let i = 0; i < dirs.length; i++) {
    const data = fs.readFileSync(dirs[i], 'utf8');
    const obj = JSON.parse(data);
    for (const key in obj) {
      if (obj.hasOwnProperty(key) && obj[key].名称 === thing_name) {
        const str = path.basename(dirs[i]);
        const newStr = str.replace('列表.json', '');
        return newStr;
      }
    }
  }
  return false;
}
const result = findNameOne();
return result;
}

//背包添加
export async function Add_bag(usr_qq, thing_name, number) {
  let bag = await Read_bag(usr_qq);
  const thing_type = await Read_thingtype(thing_name);
  if(thing_type == false){
    return;
  }
  let bag0 = bag[thing_type].find(obj => obj["名称"] == thing_name)
  if(bag0 == undefined){
    if(thing_type == "装备"){
      let equip = await Read_equiplist(usr_qq);
      let equip0 = equip.find(obj => obj["名称"] == thing_name);
      equip0["数量"] = 1;
      bag[thing_type].push(equip0);
    }else{
      bag[thing_type].push({ "名称": thing_name, "数量": number });
    }
  }else{
    bag0["数量"] = Number(bag0["数量"]) + Number(number);
  }
  for (let i = 0; i < bag[thing_type].length; i++) {
  if (bag[thing_type][i]["数量"] == 0) {
    bag[thing_type].splice(i, 1);
    i--;
  }
}
  await Write_bag(usr_qq, bag);
  return;
}

//读取battle战斗对象
export async function Read_battleobject(usr_qq) {
  let dir = path.join(`${__PATH.battle_path}/战斗对象/${usr_qq}.json`);
  let object = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  object = JSON.parse(object);
  return object;
}

//读取battle战斗属性
export async function Read_battleplayer(usr_qq) {
  let dir = path.join(`${__PATH.battle_path}/战斗属性/${usr_qq}.json`);
  let player = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  player = JSON.parse(player);
  return player;
}

//写入battle战斗对象
export async function Write_battleobject(usr_qq, object) {
  let dir = path.join(`${__PATH.battle_path}/战斗对象/${usr_qq}.json`);
  let new_ARR = JSON.stringify(object, '', '\t');
  fs.writeFileSync(dir, new_ARR, 'utf8', err => {
    console.log('写入成功', err);
  });
  return;
}

//写入battle战斗属性
export async function Write_battleplayer(usr_qq, player) {
  let dir = path.join(`${__PATH.battle_path}/战斗属性/${usr_qq}.json`);
  let new_ARR = JSON.stringify(player, '', '\t');
  fs.writeFileSync(dir, new_ARR, 'utf8', err => {
    console.log('写入成功', err);
  });
  return;
}

//battle计算增益
export async function battle_add(player) {
  for(let i = 0; i < player["增益"].length; i++){
    player[player["增益"][i]["效果"]] *= (1 + player["增益"][i]["效果"])
  }
  return player;
}

//是否暴击
export async function ifbaoji(num1, num2) {
  const x = num1 / (num1 + num2);
  const randomNum = Math.random(); 
if (randomNum <= x) {
  return true;
} else {
  return false;
}
}

//是否命中
export async function ifmingzhong(num1, num2) {
  const x = num1 / (num1 + num2);
  const randomNum = Math.random(); 
if (randomNum <= x) {
  return true;
} else {
  return false;
}
}

//暴击伤害倍率
export async function baoji(num1, num2, ifbao) {
  let x = 1;
  if(ifbao){
    x = 2 * num1 / (num1 + 2 * num2) + 1;
  }else{
    x = 1;
  }
  return x;
}

//伤害计算
export async function harm_work(atk, def) {
  let x = 0;
  x = atk * atk / (atk + def) - def / 10;
    if (x < 0){
      x = 0;
    }
  return x;
}

//读技能倍率
export async function Read_skillpower(skillname) {
  let dir = path.join(`${__PATH.lib_path}/技能列表.json`);
  let skill = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  skill = JSON.parse(skill);
  const num = skill.findIndex(obj => obj.名称 === skillname);
  const power = skill[num]["倍率"]
  return power;
}

export async function Read_skilltype(skillname) {
  let dir = path.join(`${__PATH.lib_path}/技能列表.json`);
  let skill = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  skill = JSON.parse(skill);
  const num = skill.findIndex(obj => obj.名称 === skillname);
  const power = skill[num]["类别"]
  return power;
}

export async function Read_skilltime(skillname) {
  let dir = path.join(`${__PATH.lib_path}/技能列表.json`);
  let skill = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  skill = JSON.parse(skill);
  const num = skill.find(obj => obj.名称 === skillname);
  const time = num["冷却"];
  return time;
}

export async function Read_skillmp(skillname) {
  let dir = path.join(`${__PATH.lib_path}/技能列表.json`);
  let skill = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  skill = JSON.parse(skill);
  const num = skill.findIndex(obj => obj.名称 === skillname);
  const mp = skill[num]["魔耗"]
  return mp;
}

export async function Read_skill(usr_qq) {
  let dir = path.join(`${__PATH.lib_path}/技能列表.json`);
  let skill = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  skill = JSON.parse(skill);
  return skill;
}

export async function Read_skillinfornation(skill_name) {
  let dir = path.join(`${__PATH.lib_path}/技能列表.json`);
  let skill = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  skill = JSON.parse(skill);
  let index = skill.findIndex(obj => obj["名称"] == skill_name);
  return skill[index];
}


export async function battle_round(player1, object1){
  let skill_pro;
  let skill;
  let type;
    if(player1["当前技能"] !== "普攻"){
      skill = player1["技能"].find(obj => obj["技能名"] == player1["当前技能"]);
      skill_pro = skill["熟练度"];
      type = await Read_skilltype(player1["当前技能"]);
      let num0 = player1["技能"].findIndex(obj => obj["技能名"] == player1["当前技能"]);
      let mp = await Read_skillmp(player1["当前技能"]);
      if(num0 == -1){
        player1["当前技能"] = "普攻";
      }/*else{
        player1["技能"][num0]["时间"] = await Read_skilltime(player1["当前技能"]);
        player1["当前魔力"] -= mp;
      }*/
    }
  let skill0 = await Read_skillinfornation(player1["当前技能"]);
  let word1 = "";
  let word2 = "";
  let word20 = "";
  let word21 = "";
  let mingzhong = await ifmingzhong(player1["命中"], object1["闪避"]);
  let baoji1 = await ifbaoji(player1["暴击"], object1["暴免"]);
  let baoshang = await baoji(player1["暴伤"], object1["暴抗"], baoji1);



  //读取技能前生效果
  if(player1["当前技能"] !== "普攻"){
    for(let b = 0; b < skill0["效果"].length; b++){
        if(skill0["效果"][b]["名"] == "精准"){
          mingzhong = true;
          word20 += `\n#精准# 此攻击必定命中`
        }else if(skill0["效果"][b]["名"] == "痛击"){
          baoji1 = true;
          word20 += `\n#痛击# 此攻击必定暴击`
        }
    }
  }




    if(mingzhong == false){
      if(player1["当前技能"] == "普攻"){
        word1 = `[Miss]${player1["名称"]}对[${object1["名称"]}]进行普通攻击，却被闪避了！` 
      }else{
        word1 = `[Miss]${player1["名称"]}对[${object1["名称"]}]释放技能[${player1["当前技能"]}]，却被闪避了！`  
      }
    }else if(baoji1 == true){
        word1 = `[暴击!]`
    }
    let harm = 0;
    if(mingzhong){
    if(player1["当前技能"] == "普攻"){
      let atk = player1["物攻"];
      harm = await harm_work(atk, object1["物防"]);
      harm = 0.8 * harm + 0.4 * harm * Math.random();
      harm = Math.floor(harm * baoshang);
      word2 = `${player1["名称"]}对[${object1["名称"]}]进行普通攻击，造成${harm}点物理伤害`;
    }else if(type == "物理"){
      const power = await Read_skillpower(player1["当前技能"]);
      let atk = player1["物攻"] * (power / 100) * (1 + skill_pro * 0.1);
      harm = await harm_work(atk, object1["物防"]);
      harm = 0.8 * harm + 0.4 * harm * Math.random();
      harm = Math.floor(harm * baoshang);
      word2 = `${player1["名称"]}对[${object1["名称"]}]释放技能[${player1["当前技能"]}]，造成${harm}点物理伤害`;
    }else if(type == "魔法"){
      const power = await Read_skillpower(player1["当前技能"]);
      let atk = player1["魔攻"] * (power / 100) * (1 + skill_pro * 0.1);
      harm = await harm_work(atk, object1["魔防"]);
      harm = 0.8 * harm + 0.4 * harm * Math.random();
      harm = Math.floor(harm * baoshang);
      word2 = `${player1["名称"]}对[${object1["名称"]}]释放技能[${player1["当前技能"]}]，造成${harm}点魔法伤害`;
    }
    //读取技能后生效果
    if(player1["当前技能"] !== "普攻"){
      for(let a = 0; a < skill0["效果"].length; a++){
        if((skill0["效果"][a]["名"] == "物攻" || skill0["效果"][a]["名"] == "魔攻") && skill0["效果"][a]["数"] < 0){
          object1["增益"].push({"属性": skill0["效果"][a]["名"], "数值": skill0["效果"][a]["数"], "时间": skill0["效果"][a]["时"]})
          word21 += `\n#虚弱# 攻击属性降低`
        }else if((skill0["效果"][a]["名"] == "物防" || skill0["效果"][a]["名"] == "魔防") && skill0["效果"][a]["数"] < 0){
          object1["增益"].push({"属性": skill0["效果"][a]["名"], "数值": skill0["效果"][a]["数"], "时间": skill0["效果"][a]["时"]})
          word21 += `\n#脆弱# 防御属性降低`
        }else if(skill0["效果"][a]["名"] == "命中" && skill0["效果"][a]["数"] < 0){
          object1["增益"].push({"属性": skill0["效果"][a]["名"], "数值": skill0["效果"][a]["数"], "时间": skill0["效果"][a]["时"]})
          word21 += `\n#迷失# 命中属性降低`
        }else if(skill0["效果"][a]["名"] == "闪避" && skill0["效果"][a]["数"] < 0){
          object1["增益"].push({"属性": skill0["效果"][a]["名"], "数值": skill0["效果"][a]["数"], "时间": skill0["效果"][a]["时"]})
          word21 += `\n#迟缓# 闪避属性降低`
        }else if(skill0["效果"][a]["名"] == "暴击" && skill0["效果"][a]["数"] < 0){
          object1["增益"].push({"属性": skill0["效果"][a]["名"], "数值": skill0["效果"][a]["数"], "时间": skill0["效果"][a]["时"]})
          word21 += `\n#迟钝# 暴击属性降低`
        }else if(skill0["效果"][a]["名"] == "暴伤" && skill0["效果"][a]["数"] < 0){
          object1["增益"].push({"属性": skill0["效果"][a]["名"], "数值": skill0["效果"][a]["数"], "时间": skill0["效果"][a]["时"]})
          word21 += `\n#怠惰# 暴伤属性降低`
        }else if(skill0["效果"][a]["名"] == "暴免" && skill0["效果"][a]["数"] < 0){
          object1["增益"].push({"属性": skill0["效果"][a]["名"], "数值": skill0["效果"][a]["数"], "时间": skill0["效果"][a]["时"]})
          word21 += `\n#疲惫# 暴免属性降低`
        }else if(skill0["效果"][a]["名"] == "暴抗" && skill0["效果"][a]["数"] < 0){
          object1["增益"].push({"属性": skill0["效果"][a]["名"], "数值": skill0["效果"][a]["数"], "时间": skill0["效果"][a]["时"]})
          word21 += `\n#疲软# 暴抗属性降低`
        }else if(skill0["效果"][a]["名"] == "混乱" || skill0["效果"][a]["名"] == "冰冻" || skill0["效果"][a]["名"] == "眩晕" || skill0["效果"][a]["名"] == "石化"){
          object1["状态"].push({"状态": skill0["效果"][a]["名"], "时间": skill0["效果"][a]["时"]})
          word21 += `\n#${skill0["效果"][a]["名"]}# 进入${skill0["效果"][a]["名"]}状态`
        }

      }
    }
    }

    
    

    const life1 = object1["当前生命"];
    object1["当前生命"] -= harm;
    if(object1["当前生命"] < 0){
      object1["当前生命"] = 0;
    }
    const life2 = object1["当前生命"];
    const life3 = player1["当前生命"];
    const life4 = player1["当前生命"];
    let word3 = `\n[${object1["名称"]}]当前生命:✘${life1}→${life2}✘\n[${player1["名称"]}]当前生命:✘${life3}→${life4}✘`
    let word = word1 + word2 + word20 + word21 + word3;
   return word;
}

export async function delete_battleobject(usr_qq){
  const filePath = path.join(__PATH.battle_path, '战斗对象', `${usr_qq}.json`);
  // 删除文件
  fs.unlink(filePath, (err) => {
    if (err) throw err;
    console.log(`${filePath} 已成功删除`);
  });
}

export async function delete_battleplayer(usr_qq){
  const filePath = path.join(__PATH.battle_path, '战斗属性', `${usr_qq}.json`);
  // 删除文件
  fs.unlink(filePath, (err) => {
    if (err) throw err;
    console.log(`${filePath} 已成功删除`);
  });
}

export async function Read_proplist(usr_qq) {
  let dir = path.join(`${__PATH.lib_path}/物品列表/道具列表.json`);
  let prop = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  prop = JSON.parse(prop);
  return prop;
}

export async function Read_equiplist(usr_qq) {
  let dir = path.join(`${__PATH.lib_path}/物品列表/装备列表.json`);
  let equip = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  equip = JSON.parse(equip);
  return equip;
}

export async function my_equip(usr_qq) {
        let player = await Read_player(usr_qq);
        let player_equ = await Read_equipment(usr_qq);
        const wuqi = player_equ["武器"]["名称"]||"[未装备]";
        const fushou = player_equ["副手"]["名称"]||"[未装备]";
        const toujian = player_equ["头肩"]["名称"]||"[未装备]";
        const shangzhuang = player_equ["上装"]["名称"]||"[未装备]";
        const xiazhuang = player_equ["下装"]["名称"]||"[未装备]";
        const yaodai = player_equ["腰带"]["名称"]||"[未装备]";
        const xiezi = player_equ["鞋子"]["名称"]||"[未装备]";
        const jiezhi = player_equ["戒指"]["名称"]||"[未装备]";
        const shouzhuo = player_equ["手镯"]["名称"]||"[未装备]";
        const xianglian = player_equ["项链"]["名称"]||"[未装备]";
        const word = `===[${player.昵称}]的装备===\n————————————\n武器: ${wuqi}\n副手: ${fushou}\n头肩: ${toujian}\n上装: ${shangzhuang}\n腰带: ${yaodai}\n下装: ${xiazhuang}\n鞋子: ${xiezi}\n戒指: ${jiezhi}\n手镯: ${shouzhuo}\n项链: ${xianglian}`
        return word;
}

export async function Run_skilltime(player, x){
  const num = player["技能"].length;
  for(let i = 0; i < num; i++){
    if(player["技能"]["时间"] == undefined || player["技能"]["时间"] == null){
      player["技能"]["时间"] = 0;
    }
    if(player["技能"]["时间"] > 0){
      player["技能"]["时间"] -= 1 * x;
    }
  }
  return player;
}

export async function Read_team(usr_qq) {
  let dir = path.join(`${__PATH.battle_path}/组队信息.json`);
  let team = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  team = JSON.parse(team);
  return team;
}

export async function Write_team(usr_qq, team) {
  let dir = path.join(`${__PATH.battle_path}/组队信息.json`);
  let new_ARR = JSON.stringify(team, '', '\t');
  fs.writeFileSync(dir, new_ARR, 'utf8', err => {
    console.log('写入成功', err);
  });
  return;
}

export async function object_time(usr_qq, e) {
  for(let i = 0; i < 1100; i++){
    let team = await Read_team(usr_qq);
    let index = await Read_teamIndex(usr_qq);
    let team0 = [usr_qq];
    for(let c = 0; c < team.length; c++){
    team0.push(team[index]["队员"][c]["名称"])
    }
    let finish = true;
    let player = "";
    for(let a of team0){
      player = await Read_battleplayer(a);
      if(player["当前生命"] > 0){
        finish = false;
      }
    }
    if(finish){
      let object = await Read_battleobject(usr_qq);
      let word1 = "";
      let index1 = object["舞台"].findIndex(obj => obj["回合"] == "结束");
      word1 += `【${object["名称"]}】${object["舞台"][index1]["台词"]}\n————————————\n【队伍[${team[index]["队伍名"]}]战斗失败！】`;
      e.reply(word1);
      for(let j of team0){
        await Write_state(j, "自由");
      }
      break;
    }
    if(team[index]["对象"]["被击"] == 1){
      break;
    }
    if(i % 100 == 0){
      await object_attack(team[index]["队长"]["名称"], e);
    }
    //技能时间流逝
    let object0 = await Read_battleobject(usr_qq);
    await Run_skilltime(object0, 0.01);
    await Write_battleobject(usr_qq, object0);
    for(let b of team0){
      let player0 = await Read_battleplayer(b);
      await Run_skilltime(player0, 0.01);
      await Write_battleplayer(b, player0);
    }
    //回合时间流逝
      team[index]["队长"]["时间"] -= 0.1;
      if(team[index]["队长"]["时间"] < 0){
        team[index]["队长"]["时间"] = 0;
      }
      /*team = team[index]["队员"].map(item => {
      item.时间 -= 0.1;
      if(item.时间 < 0){
        item.时间 = 0;
      }
      return item;
      });*/
      
    await Write_team(usr_qq, team);
    await sleep(99);
  }
}

export async function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

export async function Read_teamIndex(usr_qq) {
  let team = await Read_team(usr_qq);
  for(let i = 0; i < team.length; i++){
    if(team[i]["队长"]["名称"] == usr_qq || team[i]["队员"].findIndex(obj => obj["名称"] == usr_qq) >= 0){
      return i;
    }
  }
  return -1;
}

export async function object_attack(usr_qq, e) {
  let team = await Read_team(usr_qq);
  let index = await Read_teamIndex(usr_qq);
  let team0 = [usr_qq];
  for(let i = 0; i < team[index]["队员"].length; i++){
    team0.push(team[index]["队员"][i]["名称"])
    }
  let object = await Read_battleobject(usr_qq);
  let word1 = "";
  let skill = "";
  let huihe = object["回合"];
  let index1 = object["舞台"].findIndex(obj => obj["回合"] == huihe);
  if(index1 >= 0){
    word1 += `【${object["名称"]}】${object["舞台"][index1]["台词"]}\n————————————\n`;
    skill = object["舞台"][index1]["技能"];
    const index2 = object["技能"].findIndex(obj => obj["技能名"] == skill);
    //技能数组
    skill = object["技能"][index2];
  }else{
    const ran1 = Math.floor(Math.random() * object["技能"].length);
    //技能数组
    skill = object["技能"][ran1];
  }
  object["当前技能"] = skill["技能名"];
  await Write_battleobject(usr_qq, object);
  let skill0 = await Read_skillinfornation(skill["技能名"]);
  let player = "";
  let player1 = "";
  let object1 = "";
  let word2 = "";
  let word3 = "";
  let word4 = "";
  let word41 = "";
  let word42 = "";
  let word5 = "";
  let ran1 = "";
  let ran2 = "";
  let index2 = "";
  let shuzhi2 = "";
  let shijian2 = "";
  let num = "";
  let finish = false;
  if(skill0["效果"].findIndex(obj => obj["名"] == "全域") >= 0){
    word2 = `[${object["名称"]}]发动了技能[${skill["技能名"]}]\n#全域# 造成全体伤害`;
    for(let a = 0; a < team0.length; a++){
      player = await Read_battleplayer(team0[a]);
      if(player["当前生命"] == 0){
        word5 += `\n•[${player["名称"]}]已倒地`;
        continue;
      }
      player1 = await battle_add(player);
      object1 = await battle_add(object);
      word3 = await battle_round(object1, player1);
      player["当前生命"] = player1["当前生命"];
      player["当前魔力"] = player1["当前魔力"];
      player["增益"] = player1["增益"];
      player["状态"] = player1["状态"];
      await Write_battleplayer(team0[a], player);
      object["当前生命"] = object1["当前生命"];
      object["当前魔力"] = object1["当前魔力"];
      object["增益"] = object1["增益"];
      object["状态"] = object1["状态"];
      object["技能"] = object1["技能"];
      await Write_battleobject(usr_qq, object);
      word4 = word3.split('\n');
      word42 = word4.filter(line => line.includes('#'));
      word42 = word42.join('\n');
      if(word42 !== ""){
        word42 = `\n` + word42;
      }
      word4 = word4.slice(-2, -1)[0].trim();
      if(word3.includes("[Miss]")){
        word5 += `\n•[Miss] [${player["名称"]}]闪避了攻击！\n ` + word4 + word42;
      }else if(word3.includes("[暴击!]")){
        word41 = word3.split('造成');
        word41 = word41.slice(-1)[0].split('伤害')[0].trim();
        word5 += `\n•[暴击!] 对[${player["名称"]}]造成${word41}伤害\n ` + word4 + word42;
      }else{
        word41 = word3.split('造成');
        word41 = word41.slice(-1)[0].split('伤害')[0].trim();
        word5 += `\n•对[${player["名称"]}]造成${word41}伤害\n ` + word4 + word42;
      }
    }
  }else if(skill0["效果"].findIndex(obj => obj["名"] == "追击") >= 0){
      word2 = `[${object["名称"]}]发动了技能[${skill["技能名"]}]\n#追击# 个体攻击追加`;
      ran1 = Math.floor(Math.random() * team0.length);
      player = await Read_battleplayer(team0[ran1]);
      if(player["当前生命"] == 0){
        team0.sort(() => Math.random() - 0.5);
        for(num of team0){
          player = await Read_battleplayer(num);
          if(player["当前生命"] > 0){
            finish = false;
            break;
          }
          finish = true;
        }
      }else{
        num = team0[ran1];
      }
      if(finish){
        e.reply(word1 + word2 + word5);
        return;
      }
      index2 = skill0["效果"].findIndex(obj => obj["名"] == "追击");
      shuzhi2 = skill0["效果"][index2]["数"];
      shijian2 = skill0["效果"][index2]["时"];
      for(let b = 0; b < shijian2; b++){
      ran2 = Math.floor(Math.random() * 100);
      if(ran2 > shuzhi2 && b !== 0){
        break;
      }
      player = await Read_battleplayer(num);
      if(player["当前生命"] == 0){
        break;
      }
      object = await Read_battleobject(usr_qq);
      player1 = await battle_add(player);
      object1 = await battle_add(object);
      word3 = await battle_round(object1, player1);
      player["当前生命"] = player1["当前生命"];
      player["当前魔力"] = player1["当前魔力"];
      player["增益"] = player1["增益"];
      player["状态"] = player1["状态"];
      await Write_battleplayer(num, player);
      object["当前生命"] = object1["当前生命"];
      object["当前魔力"] = object1["当前魔力"];
      object["增益"] = object1["增益"];
      object["状态"] = object1["状态"];
      object["技能"] = object1["技能"];
      await Write_battleobject(usr_qq, object);
      word4 = word3.split('\n');
      word42 = word4.filter(line => line.includes('#'));
      word42 = word42.join('\n');
      if(word42 !== ""){
        word42 = `\n` + word42;
      }
      word4 = word4.slice(-2, -1)[0].trim();
      if(word3.includes("[Miss]")){
        word5 += `\n•§${b + 1}击§ [Miss] [${player["名称"]}]闪避了攻击！\n ` + word4 + word42;
      }else if(word3.includes("[暴击!]")){
        word41 = word3.split('造成');
        word41 = word41.slice(-1)[0].split('伤害')[0].trim();
        word5 += `\n•§${b + 1}击§ [暴击!] 对[${player["名称"]}]造成${word41}伤害\n ` + word4 + word42;
      }else{
        word41 = word3.split('造成');
        word41 = word41.slice(-1)[0].split('伤害')[0].trim();
        word5 += `\n•§${b + 1}击§ 对[${player["名称"]}]造成${word41}伤害\n ` + word4 + word42;
      }
      }

  }else if(skill0["效果"].findIndex(obj => obj["名"] == "连击") >= 0){
      word2 = `[${object["名称"]}]发动了技能[${skill["技能名"]}]\n#连击# 连续多次攻击`;
      index2 = skill0["效果"].findIndex(obj => obj["名"] == "连击");
      shuzhi2 = skill0["效果"][index2]["数"];
      shijian2 = skill0["效果"][index2]["时"];
      for(let b = 0; b < shijian2; b++){
      ran2 = Math.floor(Math.random() * 100);
      if(ran2 > shuzhi2 && b !== 0){
        break;
      }
      ran1 = Math.floor(Math.random() * team0.length);
      player = await Read_battleplayer(team0[ran1]);
      if(player["当前生命"] == 0){
        team0.sort(() => Math.random() - 0.5);
        for(num of team0){
          player = await Read_battleplayer(num);
          if(player["当前生命"] > 0){
            finish = false;
            break;
          }
          finish = true;
        }
      }else{
        num = team0[ran1];
      }
      if(finish){
        e.reply(word1 + word2 + word5);
        return;
      }
      player = await Read_battleplayer(num);
      object = await Read_battleobject(usr_qq);
      player1 = await battle_add(player);
      object1 = await battle_add(object);
      word3 = await battle_round(object1, player1);
      player["当前生命"] = player1["当前生命"];
      player["当前魔力"] = player1["当前魔力"];
      player["增益"] = player1["增益"];
      player["状态"] = player1["状态"];
      await Write_battleplayer(num, player);
      object["当前生命"] = object1["当前生命"];
      object["当前魔力"] = object1["当前魔力"];
      object["增益"] = object1["增益"];
      object["状态"] = object1["状态"];
      object["技能"] = object1["技能"];
      await Write_battleobject(usr_qq, object);
      word4 = word3.split('\n');
      word42 = word4.filter(line => line.includes('#'));
      word42 = word42.join('\n');
      if(word42 !== ""){
        word42 = `\n` + word42;
      }
      word4 = word4.slice(-2, -1)[0].trim();
      if(word3.includes("[Miss]")){
        word5 += `\n•§${b + 1}击§ [Miss] [${player["名称"]}]闪避了攻击！\n ` + word4 + word42;
      }else if(word3.includes("[暴击!]")){
        word41 = word3.split('造成');
        word41 = word41.slice(-1)[0].split('伤害')[0].trim();
        word5 += `\n•§${b + 1}击§ [暴击!] 对[${player["名称"]}]造成${word41}伤害\n ` + word4 + word42;
      }else{
        word41 = word3.split('造成');
        word41 = word41.slice(-1)[0].split('伤害')[0].trim();
        word5 += `\n•§${b + 1}击§ 对[${player["名称"]}]造成${word41}伤害\n ` + word4 + word42;
      }
      } 
      }else{
      word2 = `[${object["名称"]}]发动了技能[${skill["技能名"]}]`;
      ran1 = Math.floor(Math.random() * team0.length);
      player = await Read_battleplayer(team0[ran1]);
      if(player["当前生命"] == 0){
        team0.sort(() => Math.random() - 0.5);
        for(num of team0){
          player = await Read_battleplayer(num);
          if(player["当前生命"] > 0){
            finish = false;
            break;
          }
          finish = true;
        }
      }else{
        num = team0[ran1];
      }
      if(finish){
        e.reply(word1 + word2 + word5);
        return;
      }
      object = await Read_battleobject(usr_qq);
      player1 = await battle_add(player);
      object1 = await battle_add(object);
      word3 = await battle_round(object1, player1);
      player["当前生命"] = player1["当前生命"];
      player["当前魔力"] = player1["当前魔力"];
      player["增益"] = player1["增益"];
      player["状态"] = player1["状态"];
      await Write_battleplayer(num, player);
      object["当前生命"] = object1["当前生命"];
      object["当前魔力"] = object1["当前魔力"];
      object["增益"] = object1["增益"];
      object["状态"] = object1["状态"];
      object["技能"] = object1["技能"];
      await Write_battleobject(usr_qq, object);
      word4 = word3.split('\n');
      word42 = word4.filter(line => line.includes('#'));
      word42 = word42.join('\n');
      if(word42 !== ""){
        word42 = `\n` + word42;
      }
      word4 = word4.slice(-2, -1)[0].trim();
      if(word3.includes("[Miss]")){
        word5 += `\n•[Miss] [${player["名称"]}]闪避了攻击！\n ` + word4 + word42;
      }else if(word3.includes("[暴击!]")){
        word41 = word3.split('造成');
        word41 = word41.slice(-1)[0].split('伤害')[0].trim();
        word5 += `\n•[暴击!] 对[${player["名称"]}]造成${word41}伤害\n ` + word4 + word42;
      }else{
        word41 = word3.split('造成');
        word41 = word41.slice(-1)[0].split('伤害')[0].trim();
        word5 += `\n•对[${player["名称"]}]造成${word41}伤害\n ` + word4 + word42;
      }
  }
object = await Read_battleobject(usr_qq);
object["技能"].forEach(obj => {
  obj["时间"] -= 1;
  if(obj["时间"] < 0){
  obj["时间"] = 0;
  }
})
object["回合"] += 1;
await Write_battleobject(usr_qq, object);
e.reply(word1 + word2 + word5);
return;
}


export async function Read_makelist(usr_qq) {
  let dir = path.join(`${__PATH.lib_path}/合成列表.json`);
  let make = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  make = JSON.parse(make);
  return make;
}

export async function Read_decomposelist(usr_qq) {
  let dir = path.join(`${__PATH.lib_path}/分解列表.json`);
  let decompose = fs.readFileSync(dir, 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return 'error';
    }
    return data;
  });
  //将字符串数据转变成数组格式
  decompose = JSON.parse(decompose);
  return decompose;
}